package com.atbeijing.D01;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 流处理
 */
public class WordCountFromSocket {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //数据源
        DataStreamSource<String> dss = env.socketTextStream("172.20.10.3", 9999);

        //map转换
        SingleOutputStreamOperator<WordWithCount> mappedStream = dss.flatMap(new FlatMapFunction<String, WordWithCount>() {
            @Override
            public void flatMap(String s, Collector<WordWithCount> collector) throws Exception {
                String[] words = s.split(" ");
                for (String word : words) {
                    collector.collect(new WordWithCount(word, 1L));
                }
            }
        });

        //shuffle归纳
        KeyedStream<WordWithCount, String> keyedStream = mappedStream.keyBy(new KeySelector<WordWithCount, String>() {
            @Override
            public String getKey(WordWithCount wordWithCount) throws Exception {
                return wordWithCount.word;
            }
        });

        //reduce 聚合
        SingleOutputStreamOperator<WordWithCount> reducedStream = keyedStream.reduce(new ReduceFunction<WordWithCount>() {
            @Override
            public WordWithCount reduce(WordWithCount t1, WordWithCount t2) throws Exception {
                return new WordWithCount(t1.word, t1.count + t2.count);
            }
        });

        reducedStream.print();
        env.execute();
    }

    public static class WordWithCount{
        public String word;
        public long count;

        @Override
        public String toString() {
            return "WordWithCount{" +
                    "word='" + word + '\'' +
                    ", count=" + count +
                    '}';
        }

        public WordWithCount(String word, long count) {
            this.word = word;
            this.count = count;
        }

        public WordWithCount() {
        }
    }
}
